import { baseURL } from "@/env";
import { request } from "@/utils/requst";

/*函数节流*/
export function throttle(fn, interval) {
  var enterTime = 0;//触发的时间
  var gapTime = interval || 300 ;//间隔时间，如果interval不传，则默认300ms
  return function() {
    var context = this;
    var backTime = new Date();//第一次函数return即触发的时间
    if (backTime - enterTime > gapTime) {
      fn.call(context,arguments);
      enterTime = backTime;//赋值给第一次触发的时间，这样就保存了第二次触发的时间
    }
  };
}
 
/*函数防抖*/
export function debounce(func, wait, immediate) {
    let timeout;
  
    return function executedFunction(...args) {
      const context = this;
  
      const later = function() {
        timeout = null;
  
        if (!immediate) {
          func.apply(context, args);
        }
      };
  
      const callNow = immediate && !timeout;
  
      clearTimeout(timeout);
  
      timeout = setTimeout(later, wait);
  
      if (callNow) {
        func.apply(context, args);
      }
    };
  }
// 格式化时间
export function dateFormat(date:Date):string { // author: meizz
    const y = date.getFullYear()
    const m = date.getMonth() + 1
    const d = date.getDate()
    function addString(number:number): string{
        let str = ''
        return number < 10 ? '0' + number : number + ''
    }
    return y + '-' + addString(m) + '-' + addString(d)
}

export function login(){
    console.log(111)
    wx.login({
        async success(e) {
            const result:any = await request('POST', `/auth`,{code: e.code})
            wx.setStorageSync('token', result.data.data.token)
        }
    })
}
